home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / mydeviceloop / mydeviceloop.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.0 KB  |  212 lines

  1. /*
  2.     File:        MyDeviceLoop.c
  3.  
  4.     Contains:    This snippet shows how to write a device loop that            
  5.                 works under System 7 and pre-7.0 systems.  As                
  6.                 described on pages 21-23 and 21-24 of Inside Mac            
  7.                 volume VI, a device loop procedure searches all                
  8.                 active screen devices, calling a drawing procedure            
  9.                 whenever it encounters a screen that intersects            
  10.                 the drawing region.  In this app the drawing                
  11.                 region is the app's window bounds and the chosen            
  12.                 drawing procedure simply displays the screen's                
  13.                 colors for every device the window bounds intersect.        
  14.  
  15.     Written by: Edgar Lee    
  16.  
  17.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  18.  
  19.                 You may incorporate this Apple sample source code into your program(s) without
  20.                 restriction. This Apple sample source code has been provided "AS IS" and the
  21.                 responsibility for its operation is yours. You are not permitted to redistribute
  22.                 this Apple sample source code as "Apple sample source code" after having made
  23.                 changes. If you're going to re-distribute the source, we require that you make
  24.                 it clear in the source that the code was descended from Apple sample source
  25.                 code, but that you've made changes.
  26.  
  27.     Change History (most recent first):
  28.                 7/12/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  29.                 
  30.  
  31. */
  32.  
  33. #include <Dialogs.h>
  34. #include <Fonts.h>
  35.  
  36. /* Constant Declarations */
  37.  
  38. #define    WWIDTH        400
  39. #define    WHEIGHT        256
  40.  
  41. #define WLEFT        (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  42. #define WTOP        (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  43.  
  44.  
  45. enum PointSelector {topLeft,
  46.             botRight};
  47. /* Global Variable Definitions */
  48.  
  49. WindowPtr    gWindow;
  50.  
  51. void initMac();
  52. void createWindow();
  53. void doMyDeviceLoop();
  54. void doDraw();
  55. void doEventLoop();
  56.  
  57. void main(void)
  58. {
  59.     initMac();
  60.     
  61.     createWindow();
  62.  
  63.     doEventLoop();
  64. }
  65.  
  66. void initMac()
  67. {
  68.     MaxApplZone();
  69.  
  70.     InitGraf( &qd.thePort );
  71.     InitFonts();
  72.     InitWindows();
  73.     InitMenus();
  74.     TEInit();
  75.     InitDialogs( nil );
  76.     InitCursor();
  77.     FlushEvents( 0, everyEvent );
  78. }
  79.  
  80. void createWindow()
  81. {
  82.     Rect rect;
  83.     
  84.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  85.     gWindow = NewCWindow( 0L, &rect, "\pMyDeviceLoop", true, documentProc,
  86.                             (WindowPtr)-1L, true, 0L );                        
  87.     SetPort( gWindow );
  88.     
  89.     TextFont( kFontIDTimes );
  90.     TextSize( 48 );
  91.     TextMode( srcCopy );
  92. }
  93.  
  94. void doMyDeviceLoop()
  95. {
  96.     int            depth;
  97.     Rect        gDeviceRect;
  98.     Rect        intersectingRect;
  99.     GDHandle    gDevice;
  100.     //Point        point;
  101.     
  102.     /* Get the handle to the first device in the list. */
  103.     gDevice = GetDeviceList();
  104.     
  105.     /* Loop through all the devices in the list. */
  106.     while (gDevice != nil)
  107.     {
  108.         /* Get the device's gdRect and convert it to local coordinates. */
  109.         gDeviceRect = (**gDevice).gdRect;
  110.         depth = (**(**gDevice).gdPMap).pixelSize;
  111.             
  112.         GlobalToLocal( topLeft );
  113.         GlobalToLocal( (Point *)botRight );
  114.         
  115.         /* Check if the app's window rect intersects the device's, and if it */
  116.         /*    does, set the clip region's rect to the intersection, then DRAW! */
  117.         
  118.         if (SectRect( &gWindow->portRect, &gDeviceRect, &intersectingRect ))
  119.         {
  120.             ClipRect( &intersectingRect );
  121.             doDraw( depth, &intersectingRect );
  122.         }
  123.         
  124.         /* Get the next device in the list. */
  125.         gDevice = GetNextDevice( gDevice );
  126.     }
  127. }
  128.  
  129. void doDraw( depth)
  130. int        depth;
  131. {
  132.     int                i;
  133.     int                totalColors;
  134.     int                penThickness;
  135.     //RGBColor        color;
  136.     CTabHandle        ctable;
  137.     Str255            string = "\pDirect Colors";
  138.     
  139.     if (depth > 8)
  140.     {
  141.         BackColor( blackColor );
  142.         ForeColor( blueColor );
  143.         
  144.         /* Draw text for direct colors mode. */
  145.         EraseRect( &gWindow->portRect );
  146.         MoveTo( (gWindow->portRect.right - StringWidth( string )) / 2, 130 );
  147.         DrawString( string );
  148.     }
  149.     else
  150.     {
  151.         /* Get the colortable at this depth. */
  152.         ctable = GetCTable( depth );
  153.         
  154.         /* Set the line thickness to a fraction of the window height. */
  155.         totalColors = (2 << depth) / 2;
  156.         penThickness = gWindow->portRect.bottom / totalColors;
  157.         PenSize( 1, penThickness );
  158.     
  159.         /* Now draw the colors at this depth. */
  160.         for (i = 0; i < totalColors; i++)
  161.         {
  162.             RGBForeColor( &(**ctable).ctTable[i].rgb );
  163.             MoveTo( 0, i * penThickness );
  164.             LineTo( gWindow->portRect.right, i * penThickness );
  165.         }
  166.         
  167.         /* Release the colortable memory. */
  168.         DisposeCTable( ctable );
  169.     }
  170. }
  171.  
  172. void doEventLoop()
  173. {
  174.     EventRecord event;
  175.     WindowPtr   window;
  176.     short       clickArea;
  177.     Rect        screenRect;
  178.  
  179.     for (;;)
  180.     {
  181.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  182.         {
  183.             if (event.what == mouseDown)
  184.             {
  185.                 clickArea = FindWindow( event.where, &window );
  186.                 
  187.                 if (clickArea == inDrag)
  188.                 {
  189.                     screenRect = (**GetGrayRgn()).rgnBBox;
  190.                     DragWindow( window, event.where, &screenRect );
  191.                 }
  192.                 else if (clickArea == inContent)
  193.                 {
  194.                     if (window != FrontWindow())
  195.                         SelectWindow( window );
  196.                 }
  197.                 else if (clickArea == inGoAway)
  198.                     if (TrackGoAway( window, event.where ))
  199.                         return;
  200.             }
  201.             else if (event.what == updateEvt)
  202.             {
  203.                 window = (WindowPtr)event.message;    
  204.                 SetPort( window );
  205.                 
  206.                 BeginUpdate( window );
  207.                 doMyDeviceLoop();
  208.                 EndUpdate( window );
  209.             }
  210.         }
  211.     }
  212. }